package snippet;

import java.io.File;
import java.io.PrintStream;
import java.util.Map;

import be.ac.ulb.mlg.utils.Measure;
import be.ac.ulb.mlg.utils.Measurer;
import be.ac.ulb.mlg.utils.measure.*;
import be.ac.ulb.mlg.utils.measure.entropy.SchurmannGrassbergerEntropy;

public class TestMutualInfoSGestimator extends TestMeasures{
	public static final String COSTELLO			= "Costello_2009_oral.txt";
	public static final String MUELLER_KOCH		= "MuellerKoch_reactor_timeseries_cellcounts.txt";
	
	public static final String DEFAULT_DATA		= MUELLER_KOCH;
	
	/*
	 library('minet')
	 estimator = 'sg'#'shrink'#'sg'
	 A = c(9, 5, 11, 12, NA)
	 B = c(6, 5, 4, 5, 2)
	 
	 U = data.frame(B,A)
	 Hyx = entropy(U, method=estimator)
	 Hx = entropy(A, method=estimator)
	 Hy = entropy(B, method=estimator)
	 res = Hx+Hy-Hyx
	 if(res < 0) { res=0}
	 print(res)
	 */
	public static void main(String[] args)throws Exception{
		
		final boolean autoparams = args.length != 2;
		final Measure measure = new MutualInformation(new SchurmannGrassbergerEntropy());
		
		if(autoparams){
			System.out.println("No arguments, using default");
		}
		
		final String INPUT	= args[0]/*System.getProperty("user.dir")+File.separator+"src"+File.separator+"snippet"+File.separator+(autoparams?DEFAULT_DATA:args[0])*/;
		final String OUTPUT	= autoparams?null:args[1];
		
		System.out.println("Java input: "+INPUT);
		System.out.println("Java output: "+OUTPUT);
		
		CSVLoader loader = new CSVLoader(autoparams);
		if(autoparams) loader.ignore(0);
		loader.load(INPUT);
		
		final double[][] inputData = /*autoparams?*/reverse(loader.data)/*:loader.data*/;
		System.out.println("Java input: "+inputData.length+" x "+inputData[0].length);

		@SuppressWarnings("unused")
		final int N = inputData.length, M = inputData[0].length;

		Map<Measure,double[][]> result = null;
		
		Measurer measurer = new Measurer(false,M+1,null);
		measurer.useShuffling(false);
		measurer.usePreShuffle(false);
		measurer.setNumberOfThreads(1);
		measurer.setHandelingMissingValues(true);
		result = measurer.measure(inputData, new Measure[]{measure});
		
		final double[][] outputData		= upper2full(result.values().iterator().next());
		System.out.println("Java output: "+outputData.length+" x "+outputData[0].length);

		CSVSaver saver = new CSVSaver(outputData);
		//new File(OUTPUT).createNewFile();
		saver.save(OUTPUT == null?System.out:new PrintStream(OUTPUT));
	}
}
